import { App } from '@/app/app';
import { ILayoutOpts, IParam, LayoutUtil } from '@/core';
import { ProjectSetting } from '@/setting';
import { Ref } from 'vue';

/**
 * 安装应用
 *
 * @export
 */
export function useInstallApp() {
	(window as any).App = App.getInstance();
}

/**
 * 获取静态资源路径
 *
 * @export
 * @param {string} path
 */
export function useAssetPath(path: string) {
	if (import.meta.env.DEV) {
		return path;
	} else {
		if (ProjectSetting.routeMode === 'HISTORY') {
			return `/${ProjectSetting.resourceBaseUrl}${path}`;
		} else {
			if (ProjectSetting.resourceBaseUrl) {
				if (ProjectSetting.resourceBaseUrl === './') {
					return path;
				} else {
					return `/${ProjectSetting.resourceBaseUrl}${path}`;
				}
			} else {
				return path;
			}
		}
	}
}

/**
 * 使用容器布局逻辑,获得如下返回值
 *
 * @param layoutOpts 栅格布局时作为子元素的占位和偏移
 * @returns
 */
export function useLayout(layoutOpts: Ref<ILayoutOpts>) {
	// 创建新的ref对象
	const layoutResult: Ref<{
		rowStyle: IParam;
		colStyle: IParam;
		colGridOpts: any;
	}> = ref({
		rowStyle: {},
		colStyle: {},
		colGridOpts: {},
	});
	watch(
		layoutOpts,
		(newVal, oldVal) => {
			layoutResult.value.rowStyle = LayoutUtil.getRowFlexStyle(newVal);
			layoutResult.value.colStyle = LayoutUtil.getColFlexStyle(newVal);
			layoutResult.value.colGridOpts = LayoutUtil.getGridOptions(newVal);
		},
		{
			immediate: true,
			deep: true,
		}
	);
	return layoutResult;
}
